package com.ziyun.pop.web.modules.storeMaterials.controller;


import com.ziyun.pop.common.e.WhetherEnum;
import com.ziyun.pop.common.e.material.*;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeMaterials.entity.ErpStockEntity;
import com.ziyun.pop.modules.storeMaterials.entity.StoreMaterialOutDetailEntity;
import com.ziyun.pop.modules.storeMaterials.entity.StorematerialinOutEntity;
import com.ziyun.pop.modules.storeMaterials.entity.dto.StorematerialinOutDto;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import com.ziyun.pop.modules.storeMaterials.service.StoreMaterialInOutService;
import com.ziyun.pop.modules.storeMaterials.service.StoreMaterialOutDetailService;
import com.ziyun.pop.modules.supp.entity.SellerInfoEntity;
import com.ziyun.pop.modules.supp.service.SellerInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/10/09 09:51
 * @Version V1.0
 *
 * 门店调拨出库
 */
@RestController
@RequestMapping("storeMaterials/storematerialinOut")
public class StoreMaterialInOutController {
	@Autowired
	private StoreMaterialInOutService storeMaterialInOutService;

	@Autowired
	private StoreMaterialOutDetailService storeMaterialOutDetailService;

	@Autowired
	private ErpStockService erpStockService;

	@RequestMapping("/list")
	public R list(@RequestParam Map<String,Object> params) {
		Long sellerId = UserUtils.getCurentUser().getSellerId();
		Integer stockId = erpStockService.getStockId(sellerId);
		params.put("stockId",stockId);
		PageUtils page = storeMaterialInOutService.queryPageList(params);
		List<SellerInfoEntity> selectLists = storeMaterialInOutService.getSelectList();
		Map<String,Object> userData=new HashMap<>();
		userData.put("selectLists",selectLists);
		return R.ok().put("page", page).put("userData",userData);
	}

	//  根据ID把单价查询出来
	@RequestMapping("/getPrice/{id}")
	public R getCost(@PathVariable("id") String materialId) {
		Long sellerId = UserUtils.getCurentUser().getSellerId();
		Integer stockId = erpStockService.getStockId(sellerId);
		BigDecimal price = storeMaterialInOutService.getPrice(materialId,stockId);
		return R.ok().put("price",price);
	}
	
	/**
	 * @author   Alix_zw
	 * @Description   提交申请的Controller
	**/

	@RequestMapping("/save")
	public R save(@RequestBody StorematerialinOutDto storematerialinOutDto){

		Integer resultStockId;
		try {
			resultStockId = storeMaterialInOutService.getStockIdBySellerId(UserUtils.getCurentUser().getSellerId().intValue());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("门店ID有误");
		}
		storematerialinOutDto.setSellerId(UserUtils.getCurentUser().getSellerId().intValue());
		storematerialinOutDto.setStockId(resultStockId);
		StorematerialinOutEntity storematerialinOutEntity = storematerialinOutDto.getStorePurchaseApplyEntity();

		storematerialinOutEntity.setStockId(resultStockId);

		storematerialinOutEntity.setCreateName(UserUtils.getCurentUser().getUsername());

		storematerialinOutDto.setStorePurchaseApplyEntity(storematerialinOutEntity);

		String response = JsonUtils.object2String(storeMaterialInOutService.insertApply(storematerialinOutDto));

		R rs = JsonUtils.string2Object(response, R.class);
		return  rs;
	}

	
	/**
	 * @author   Alix_zw
	 * @Date
	 * @Description   出库审核
	 * @Return    
	**/
	@RequestMapping("/audit/{id}")
	public R audit(@PathVariable("id") Integer outId){

		return storeMaterialInOutService.audit(outId);

	}


	/**
	 * @author   Alix_zw
	 * @Description   出库驳回
	 
	 * @Return    
	**/

	@RequestMapping("/reject/{id}")
	public R reject(@PathVariable("id") Integer outId){
		return storeMaterialInOutService.reject(outId);
	}

		/**
		 * 获取枚举类
		 */
		@RequestMapping("/getEnum")
		public R getEnum () {
			List<Map<String, Object>> whetherEnum = JsonUtils.enumToList(WhetherEnum.class);
			List<Map<String, Object>> stockMaterialIoTypeEnum = JsonUtils.enumToList(StockMaterialIoTypeEnum.class);
			List<Map<String, Object>> directionEnum = JsonUtils.enumToList(DirectionEnum.class);
			List<Map<String, Object>> storeMaterialInStatusEnum = JsonUtils.enumToList(StoreMaterialInStatusEnum.class);
			List<Map<String, Object>> financePaymentStatusEnum = JsonUtils.enumToList(FinancePaymentStatusEnum.class);
			List<Map<String, Object>> storeMaterialOutInStatusEnum = JsonUtils.enumToList(StoreMaterialOutInStatusEnum.class);


			return R.ok()
					.put("whetherEnum", whetherEnum)
					.put("stockMaterialIoTypeEnum", stockMaterialIoTypeEnum)
					.put("directionEnum", directionEnum)
					.put("storeMaterialInStatusEnum", storeMaterialInStatusEnum)
					.put("storeMaterialOutInStatusEnum",storeMaterialOutInStatusEnum)
					.put("financePaymentStatusEnum", financePaymentStatusEnum);

		}
	@RequestMapping("/info/{outId}")
	public R getInfo(@PathVariable("outId") Integer outId){
		Integer stockByOutId = storeMaterialInOutService.getStockByOutId(outId);
		String stockName = erpStockService.getStockName(stockByOutId);
		StorematerialinOutEntity infoData = storeMaterialInOutService.getInfoData(outId);
		return R.ok().put("infoData",infoData).put("stockName",stockName);
	}


	@RequestMapping("/storematerialinOutList/{outId}")
	public R storematerialinOutList(@PathVariable("outId") Integer outId){

		String outNO = storeMaterialInOutService.getOutNoByOutId(outId);

		List<StoreMaterialOutDetailEntity> liststoreMaterialOutDetail = storeMaterialOutDetailService.getListstoreMaterialOutDetail(outId, outNO);

		return R.ok().put("liststoreMaterialOutDetail",liststoreMaterialOutDetail);


	}







}
